package org.rsbot.client.input;

import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.event.FocusEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.DirectColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.util.Hashtable;
import javax.swing.SwingUtilities;
import org.rsbot.Application;
import org.rsbot.bot.Bot;
import org.rsbot.bot.RSLoader;

public class Canvas extends java.awt.Canvas
{
  public static final int GRAPHICS_DELAY = 6;
  public static final int SLOW_GRAPHICS_DELAY = 50;
  private static final long serialVersionUID = -2276037172265300477L;
  private Bot bot;
  private boolean toshi;
  private boolean visible;
  private boolean focused;

  public Canvas()
  {
    init();
  }

  public Canvas(GraphicsConfiguration paramGraphicsConfiguration) {
    super(paramGraphicsConfiguration);
    init();
  }

  public final Graphics getGraphics()
  {
    if (this.bot == null) {
      if (this.toshi) {
        return super.getGraphics();
      }
      this.bot = Application.getBot(this);
      this.toshi = true;
    }
    try
    {
      Thread.sleep(this.bot.disableRendering ? 50L : 6L);
    } catch (InterruptedException localInterruptedException) {
    }
    return this.bot.getBufferGraphics();
  }

  public final boolean hasFocus()
  {
    return this.focused;
  }

  public final boolean isValid()
  {
    return this.visible;
  }

  public final boolean isVisible()
  {
    return this.visible;
  }

  public final boolean isDisplayable()
  {
    return true;
  }

  public final Dimension getSize()
  {
    if (this.bot != null) {
      return this.bot.getLoader().getSize();
    }
    return Application.getPanelSize();
  }

  public final void setVisible(boolean paramBoolean)
  {
    super.setVisible(paramBoolean);
    this.visible = paramBoolean;
  }

  public final void setFocused(boolean paramBoolean) {
    if ((paramBoolean) && (!this.focused))
    {
      super.processEvent(new FocusEvent(this, 1004, false, null));
    } else if (this.focused)
    {
      super.processEvent(new FocusEvent(this, 1005, true, null));
    }
    this.focused = paramBoolean;
  }

  public Image createImage(int paramInt1, int paramInt2)
  {
    int[] arrayOfInt = new int[paramInt2 * paramInt1];
    DataBufferInt localDataBufferInt = new DataBufferInt(arrayOfInt, arrayOfInt.length);
    DirectColorModel localDirectColorModel = new DirectColorModel(32, 16711680, 65280, 255);
    WritableRaster localWritableRaster = Raster.createWritableRaster(localDirectColorModel.createCompatibleSampleModel(paramInt1, paramInt2), localDataBufferInt, null);

    return new BufferedImage(localDirectColorModel, localWritableRaster, false, new Hashtable());
  }

  protected final void processEvent(AWTEvent paramAWTEvent)
  {
    if (!(paramAWTEvent instanceof FocusEvent))
      super.processEvent(paramAWTEvent);
  }

  private void init()
  {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        Canvas.this.setFocused(true);
      }
    });
  }
}